<?php

namespace app\admin\controller;

use app\admin\model\Dish;
use app\admin\model\Orders;
use app\admin\model\Setmeal;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\service\OrderStatus;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $totalorder = Orders::whereTime('checkout_time', 'today')->count();
        $todayturnover = Orders::whereTime('checkout_time', 'today')->where('status', OrderStatus::COMPLETED)->sum('amount');
        $todayvalidorder = Orders::whereTime('checkout_time', 'today')->where('status', OrderStatus::COMPLETED)->count();
        $orderfulfillmentrate = 0;
        if ($todayvalidorder > 0) {
             $orderfulfillmentrate = $todayvalidorder / $totalorder / 0.01;
        }
        $averageunitprice = 0;
        if ($todayturnover > 0) {
            $averageunitprice = $todayturnover / $todayvalidorder;
        }
        $this->view->assign([
            'todayturnover' => $todayturnover,
            'todayvalidorder' => $todayvalidorder,
            'orderfulfillmentrate' => $orderfulfillmentrate,
            'averageunitprice' => $averageunitprice,
            'newusercount' => User::whereTime('jointime','today')->count(),
            'todayawaitingorder' => Orders::where('status',OrderStatus::TO_BE_CONFIRMED)->count(),
            'todaydeliveredorder' => Orders::where('status',OrderStatus::CONFIRMED)->count(),
            'todaycompletedorder' => Orders::where('status',OrderStatus::COMPLETED)->count(),
            'todaycancelorder' => Orders::where('status',OrderStatus::CANCELLED)->count(),
            'totalorder' =>   $totalorder,
            'dishsale' =>   Dish::where('status',1)->count(),
            'dishsold' =>   Dish::where('status',0)->count(),
            'setmealsale' =>   Setmeal::where('status',1)->count(),
            'setmealsold' =>   Setmeal::where('status',0)->count(),
        ]);

        return $this->view->fetch();
    }
}
